/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tcc.ejb.autenticador.controle;



import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import tcc.ejb.autenticador.controle.exceptions.NonexistentEntityException;
import tcc.ejb.autenticador.controle.exceptions.PreexistingEntityException;
import tcc.ejb.autenticador.entidade.Aplicacao;
import tcc.ejb.autenticador.entidade.Profissional;


/**
 *
 * @author Maciel
 */
public class AplicacaoJpaController {

    public AplicacaoJpaController() {
        emf = Persistence.createEntityManagerFactory("autenticador");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Aplicacao aplicacao) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Profissional profissional = aplicacao.getProfissional();
            if (profissional != null) {
                profissional = em.getReference(profissional.getClass(), profissional.getId());
                aplicacao.setProfissional(profissional);
            }
            em.persist(aplicacao);
            if (profissional != null) {
                profissional.getAplicacaoList().add(aplicacao);
                profissional = em.merge(profissional);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findAplicacao(aplicacao.getId()) != null) {
                throw new PreexistingEntityException("Aplicacao " + aplicacao + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Aplicacao aplicacao) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Aplicacao persistentAplicacao = em.find(Aplicacao.class, aplicacao.getId());
            Profissional profissionalOld = persistentAplicacao.getProfissional();
            Profissional profissionalNew = aplicacao.getProfissional();
            if (profissionalNew != null) {
                profissionalNew = em.getReference(profissionalNew.getClass(), profissionalNew.getId());
                aplicacao.setProfissional(profissionalNew);
            }
            aplicacao = em.merge(aplicacao);
            if (profissionalOld != null && !profissionalOld.equals(profissionalNew)) {
                profissionalOld.getAplicacaoList().remove(aplicacao);
                profissionalOld = em.merge(profissionalOld);
            }
            if (profissionalNew != null && !profissionalNew.equals(profissionalOld)) {
                profissionalNew.getAplicacaoList().add(aplicacao);
                profissionalNew = em.merge(profissionalNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = aplicacao.getId();
                if (findAplicacao(id) == null) {
                    throw new NonexistentEntityException("The aplicacao with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Aplicacao aplicacao;
            try {
                aplicacao = em.getReference(Aplicacao.class, id);
                aplicacao.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The aplicacao with id " + id + " no longer exists.", enfe);
            }
            Profissional profissional = aplicacao.getProfissional();
            if (profissional != null) {
                profissional.getAplicacaoList().remove(aplicacao);
                profissional = em.merge(profissional);
            }
            em.remove(aplicacao);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Aplicacao> findAplicacaoEntities() {
        return findAplicacaoEntities(true, -1, -1);
    }

    public List<Aplicacao> findAplicacaoEntities(int maxResults, int firstResult) {
        return findAplicacaoEntities(false, maxResults, firstResult);
    }

    private List<Aplicacao> findAplicacaoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Aplicacao.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Aplicacao findAplicacao(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Aplicacao.class, id);
        } finally {
            em.close();
        }
    }

    public int getAplicacaoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Aplicacao> rt = cq.from(Aplicacao.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
